package edu.cuit.avatar.dao.impl;

import edu.cuit.avatar.common.JdbcHelper;
import edu.cuit.avatar.common.JdbcTemplate;
import edu.cuit.avatar.common.handler.BeanResultHandler;
import edu.cuit.avatar.common.handler.ListBeanResultSetHandler;
import edu.cuit.avatar.dao.StudentDao;
import edu.cuit.avatar.entity.Student;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/25 16:14
 */
@Slf4j
public class StudentDaoImpl implements StudentDao {
    private JdbcHelper helper = JdbcHelper.getInstance();
    private JdbcTemplate template = JdbcTemplate.TEMPLATE;
    @Override
    public int save(Student student) {
        String sql = "insert into student(ssn,name,gender,tel,password,dept_id) values(?,?,?,?,?,?)";
        return template.executeUpdate(sql, student.getSsn(), student.getName(), student.getGender(),
                student.getTel(), student.getPassword(), student.getDeptId());
        //        var conn = helper.getConnection();
//        try {
//            var ptst = conn.prepareStatement(sql);
//            ptst.setString(1, student.getSsn());
//            ptst.setString(2, student.getName());
//            ptst.setString(3, student.getGender());
//            ptst.setString(4, student.getTel());
//            ptst.setString(5, student.getPassword());
//            ptst.setInt(6, student.getDeptId());
//
//            return ptst.executeUpdate();
//        } catch (SQLException e) {
//            log.error(e.getMessage(), e);
//            throw new RuntimeException(e);
//        } finally {
//            helper.free(conn);
//        }
    }

    @Override
    public int removeBySsn(String ssn) {
        String sql = "delete from student where ssn=?";
        return template.executeUpdate(sql, ssn);
//        var conn = helper.getConnection();
//        try {
//            var ptst = conn.prepareStatement(sql);
//            ptst.setString(1, ssn);
//
//            return ptst.executeUpdate();
//        } catch (SQLException e) {
//            log.error(e.getMessage(), e);
//            throw new RuntimeException(e);
//        } finally {
//            helper.free(conn);
//        }
    }

    @Override
    public int updateBySsn(Student student) {
        var sql = "update student set name=?, gender=?, tel=?, password=?, dept_id=? where ssn=?";

        return template.executeUpdate(sql, student.getName(), student.getGender(),
                student.getTel(), student.getPassword(), student.getDeptId(), student.getSsn());
    }

    @Override
    public Student findBySsn(String ssn) {
//        Student student = null;
        var sql = "select ssn, name, gender, tel,password, dept_id,create_time,update_time from student where ssn = ?";
//        var rs = template.executeQuery(sql, ssn);
//        try {
//            if(rs.next()){
//                student = new Student();
//                student.setSsn(rs.getString("ssn"));
//                student.setName(rs.getString("name"));
//                student.setGender(rs.getString("gender"));
//                student.setTel(rs.getString("tel"));
//                student.setPassword(rs.getString("password"));
//                student.setDeptId(rs.getInt("dept_id"));
//            }
//        } catch (SQLException e) {
//            log.error(e.getMessage(), e);
//        }
//        return student;

        return template.executeQuery(sql, new BeanResultHandler<>(Student.class), ssn);
    }

    @Override
    public List<Student> limit(int offset, int size) {
//        List<Student> students = new ArrayList<>();
        var sql = "select ssn, name, gender, tel,password, dept_id,create_time,update_time from student limit ?,?";
//        var rs = template.executeQuery(sql, offset, size);
//        try {
//            while(rs.next()){
//                var student = new Student();
//                student.setSsn(rs.getString("ssn"));
//                student.setName(rs.getString("name"));
//                student.setGender(rs.getString("gender"));
//                student.setTel(rs.getString("tel"));
//                student.setPassword(rs.getString("password"));
//                student.setDeptId(rs.getInt("dept_id"));
//                student.setCreateTime(LocalDateTime.ofInstant(rs.getDate("create_time").toInstant(), ZoneId.systemDefault()));
//                student.setUpdateTime(LocalDateTime.ofInstant(rs.getDate("update_time").toInstant(), ZoneId.systemDefault()));
//                students.add(student);
//            }
//        } catch (SQLException e) {
//            log.error(e.getMessage(), e);
//        }
//        return students;

        return template.executeQuery(sql, new ListBeanResultSetHandler<>(Student.class), offset, size);
    }

    @Override
    public long count() {
        var sql = "select count(*) from student";
        return template.executeQuery(sql, rs -> rs.next() ? rs.getLong(1) : 0);
    }
}
